package com.ruoyi.project.personal.lifephoto.controller;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.personal.lifephoto.domain.UserLifePhoto;
import com.ruoyi.project.personal.lifephoto.service.IUserLifePhotoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 生活照Controller
 * 
 * @author wyh
 * @date 2024-06-16
 */
@Api(tags = "用户生活照接口")
@RestController
@RequestMapping("/personal/photo/api")
public class UserLifePhotoApiController extends BaseController
{
    @Autowired
    private IUserLifePhotoService userLifePhotoService;

    /**
     * 查询生活照列表
     */
    @ApiOperation("用户生活照列表")
    @PreAuthorize("@ss.hasPermi('personal:photo:list')")
    @GetMapping("/list")
    public TableDataInfo list(UserLifePhoto userLifePhoto)
    {
        Long userId = SecurityUtils.getUserId();
        userLifePhoto.setUserId(userId);

        userLifePhoto.setStatus("已通过");

        startPage();
        List<UserLifePhoto> list = userLifePhotoService.selectUserLifePhotoList(userLifePhoto);
        return getDataTable(list);
    }


    /**
     * 获取生活照详细信息
     */
    @PreAuthorize("@ss.hasPermi('personal:photo:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation("获取生活照详情")
    public AjaxResult getInfo(@ApiParam(name = "id", value = "生活照id", required = true)@PathVariable("id") Long id)
    {
        return success(userLifePhotoService.selectUserLifePhotoById(id));
    }

    /**
     * 新增生活照
     */
    @PreAuthorize("@ss.hasPermi('personal:photo:add')")
    @Log(title = "生活照", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserLifePhoto userLifePhoto)
    {
        String fileName = userLifePhoto.getLifePhoto();
        if (fileName == null || fileName.isEmpty()) {
            return AjaxResult.error("文件名称不正确");
        }

        String extension = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();

        // 图片扩展名列表
        String[] imageExtensions = {"jpg", "jpeg", "png", "gif", "bmp", "webp"};
        // 视频扩展名列表
        String[] videoExtensions = {"mp4", "mov", "avi", "wmv", "flv", "mkv"};

        // 检查文件扩展名是否在图片扩展名列表中
        for (String imageExtension : imageExtensions) {
            if (extension.equals(imageExtension)) {
                userLifePhoto.setType("图片");
            }
        }

        // 检查文件扩展名是否在视频扩展名列表中
        for (String videoExtension : videoExtensions) {
            if (extension.equals(videoExtension)) {
                userLifePhoto.setType("视频");

                //设置封面
                userLifePhoto.setCover(fileName+"?vframe/jpg/offset/1");
            }
        }

        if(StringUtils.isBlank(userLifePhoto.getType())){
            return AjaxResult.error("文件格式不正确");
        }

        //获取当前用户id
        Long userId = SecurityUtils.getUserId();
        userLifePhoto.setUserId(userId);

        //空个性签名默认通过
        if(StringUtils.isBlank(userLifePhoto.getLifePhoto())){
            userLifePhoto.setStatus("已通过");
        }

        userLifePhoto.setStatus("待审核");

        return toAjax(userLifePhotoService.insertUserLifePhoto(userLifePhoto));
    }


}
